using System;
using System.Collections.Generic;
using System.Text;
using THSLib;using THSERP.BaseProvider;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

namespace THSERP.Modules.UserManagement
{
    public class UserManagementModule : BaseModuleERP
    {
        public UserManagementModule()
        {            
            Name = BaseModuleERP.cstUserManagementModule;
            InitializeModule();
        }

        public override void InitializeScreens()
        {
            guiUserManagement _guiUserManagement = new guiUserManagement();
            _guiUserManagement.ScreenNumber = "DMUM100";
            _guiUserManagement.Module = this;
            Screens.Add(_guiUserManagement);

            InitializeTreeList(_guiUserManagement.fld_trlstUserGroup);
            _guiUserManagement.AddControlsToParentScreen();
        }

        #region initialze treelist
        public void InitializeTreeList(TreeList fld_trlstUserGroup)
         {
            List<ADUserGroupsInfo> lstNode = new List<ADUserGroupsInfo>();
            fld_trlstUserGroup.Nodes.Clear();
            ADUserGroupsController objADUserGroupsController =new ADUserGroupsController();
            DataSet dsADUserGroups = objADUserGroupsController.GetAllObjects();
            if (dsADUserGroups != null)
            {
                foreach (DataRow row in dsADUserGroups.Tables[0].Rows)
                {
                    ADUserGroupsInfo objADUserGroupsInfo = (ADUserGroupsInfo)objADUserGroupsController.GetObjectFromDataRow(row);
                    if (objADUserGroupsInfo != null)
                    {
                        lstNode.Add(objADUserGroupsInfo);
                    }
                }
            }
            CreateTreeView(fld_trlstUserGroup,lstNode, null);
            fld_trlstUserGroup.ExpandAll();
        }
        public void RefreshTreeList(TreeList fld_trlstUserGroup,TreeListNode selectedNode)
        {
            List<ADUserGroupsInfo> lstNode = new List<ADUserGroupsInfo>();
            fld_trlstUserGroup.Nodes.Clear();
            ADUserGroupsController objADUserGroupsController = new ADUserGroupsController();
            DataSet dsADUserGroups = objADUserGroupsController.GetAllObjects();
            if (dsADUserGroups != null)
            {
                foreach (DataRow row in dsADUserGroups.Tables[0].Rows)
                {
                    ADUserGroupsInfo objADUserGroupsInfo = (ADUserGroupsInfo)objADUserGroupsController.GetObjectFromDataRow(row);
                    if (objADUserGroupsInfo != null)
                    {
                        lstNode.Add(objADUserGroupsInfo);
                    }
                }
            }
            CreateTreeView(fld_trlstUserGroup, lstNode, null);
            fld_trlstUserGroup.ExpandAll();
            fld_trlstUserGroup.Nodes.TreeList.FocusedNode = selectedNode;
        }
        //
        private void CreateTreeView(TreeList fld_trlstUserGroup, List<ADUserGroupsInfo> lstNodeName, DevExpress.XtraTreeList.Nodes.TreeListNode ParentNode)
        {
            fld_trlstUserGroup.BeginUnboundLoad();
            TreeListNode treeListNode;
            treeListNode = fld_trlstUserGroup.AppendNode(new object[] { "User Group",0 }, ParentNode);
            for (int i = 0; i < lstNodeName.Count; i++)
            {
                treeListNode = fld_trlstUserGroup.AppendNode(new object[] { lstNodeName[i].ADUserGroupName, 1 }, 0);
                treeListNode.Tag = lstNodeName[i].ADUserGroupID;
                treeListNode.HasChildren = HasChild(lstNodeName[i].ADUserGroupID);
      
                if (treeListNode.HasChildren)
                {
                    ADUserGroupSectionsController objUserGroupSectionsController = new ADUserGroupSectionsController();
                    DataSet dsUserGroupChild = objUserGroupSectionsController.GetUserGroupSectionByUserGroupID(lstNodeName[i].ADUserGroupID);
                    if (dsUserGroupChild != null)
                    {
                        foreach (DataRow row in dsUserGroupChild.Tables[0].Rows)
                        {
                            ADUserGroupSectionsInfo objADUserGroupSectionsInfo = (ADUserGroupSectionsInfo)objUserGroupSectionsController.GetObjectFromDataRow(row);
                            if (objADUserGroupSectionsInfo != null)
                            {
                                TreeListNode treeListChildNode = fld_trlstUserGroup.AppendNode(new object[] { objADUserGroupSectionsInfo.ADUserGroupSectionName, treeListNode.Level + 1 }, treeListNode);
                                treeListChildNode.Tag = objADUserGroupSectionsInfo.ADUserGroupSectionID;
                                AddModuleNode(fld_trlstUserGroup, objADUserGroupSectionsInfo.ADUserGroupSectionID, treeListChildNode);
                            }
                        }
                    }

                }
             }
            
            fld_trlstUserGroup.EndUnboundLoad();
        }
        private void AddModuleNode(TreeList fld_trlstUserGroup,int iUserGroupSectionID,TreeListNode parentNode)
        {
            STModuleToUserGroupSectionsController objSTModuleToUserGroupSectionsController =new STModuleToUserGroupSectionsController();
            DataSet dsModuleUserGroupSections = objSTModuleToUserGroupSectionsController.GetAllDataByForeignColumn("STUserGroupSectionID",iUserGroupSectionID);
            if (dsModuleUserGroupSections != null)
            {
                foreach (DataRow row in dsModuleUserGroupSections.Tables[0].Rows)
                {
                    STModuleToUserGroupSectionsInfo objSTModuleToUserGroupSectionsInfo = (STModuleToUserGroupSectionsInfo)objSTModuleToUserGroupSectionsController.GetObjectFromDataRow(row);
                    if (objSTModuleToUserGroupSectionsInfo != null)
                    {
                        STModulesInfo objSTModulesInfo = (STModulesInfo)new STModulesController().GetObjectByID(objSTModuleToUserGroupSectionsInfo.STModuleID);
                        STModuleDescriptionsController objModuleDescriptionsController = new STModuleDescriptionsController();
                        STModuleDescriptionsInfo objModuleDescriptionsInfo = (STModuleDescriptionsInfo)objModuleDescriptionsController.GetObjectByModuleID(objSTModulesInfo.STModuleID);
                        objSTModulesInfo.STModuleDescription = objModuleDescriptionsInfo.STModuleDescriptionDescription;
                        if (objSTModulesInfo != null)
                        {
                            TreeListNode treeListModuleNode = fld_trlstUserGroup.AppendNode(new object[] { objSTModulesInfo.STModuleDescription, parentNode.Level + 1 }, parentNode);
                            treeListModuleNode.Tag = objSTModulesInfo.STModuleID;
                        }
                    }
                }
            }
        }
        public bool HasChild(int iUserGroupID)
        {
            DataSet ds = new ADUserGroupSectionsController().GetAllDataByForeignColumn("ADUserGroupID", iUserGroupID);
            if (ds != null)
            {
                if (ds.Tables[0].Rows.Count > 0)
                    return true;
            }
            return false;
        }
        #endregion
    }
}
